package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.AddressQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.AddressDao;
import cn.org.eshow.model.Address;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("addressDao")
public class AddressDaoHibernate extends GenericDaoHibernate<Address, Integer> implements AddressDao {


    public AddressDaoHibernate() {
        super(Address.class);
    }

    @Override
    public List<Address> list(AddressQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }

    @Override
    public Page<Address> search(AddressQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setOffset(query.getOffset());
            rule.setPageSize(query.pagesize);
        }
        return page(rule);
    }

    @Override
    public Long count(AddressQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(Address.class, rule);
    }

    public EnhancedRule setRule(EnhancedRule rule, AddressQuery query) {
        if (query != null) {
            if (query.startAddTime != null) {
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            }
            if (query.endAddTime != null) {
                rule.add(Restrictions.le("addTime", query.endAddTime));
            }
            if (query.startUpdateTime != null) {
                rule.add(Restrictions.ge("updateTime", query.startUpdateTime));
            }
            if (query.endUpdateTime != null) {
                rule.add(Restrictions.le("updateTime", query.endUpdateTime));
            }
            if (StringUtils.isNotEmpty(query.addr)) {
                rule.add(Restrictions.like("addr", query.addr, MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotEmpty(query.zip)) {
                rule.add(Restrictions.like("zip", query.zip, MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotEmpty(query.name)) {
                rule.add(Restrictions.like("name", query.name, MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotEmpty(query.mobile)) {
                rule.add(Restrictions.like("mobile", query.mobile, MatchMode.ANYWHERE));
            }
            if (query.id != null) {
                rule.add(Restrictions.eq("id", query.id));
            }
            if (query.lng != null) {
                rule.add(Restrictions.eq("lng", query.lng));
            }
            if (query.lat != null) {
                rule.add(Restrictions.eq("lat", query.lat));
            }
            if (StringUtils.isNotEmpty(query.weixin)) {
                rule.add(Restrictions.like("weixin", query.weixin, MatchMode.ANYWHERE));
            }
            if (query.ifDefault != null) {
                rule.add(Restrictions.eq("ifDefault", query.ifDefault));
            }
            if (query.enabled != null) {
                rule.add(Restrictions.eq("enabled", query.enabled));
            }
            if (query.lat != null) {
                rule.add(Restrictions.eq("lat", query.lat));
            }
            if (query.province != null) {
                rule.add(Restrictions.like("province", query.province, MatchMode.ANYWHERE));
            }
            if (query.city != null) {
                rule.add(Restrictions.like("city", query.city, MatchMode.ANYWHERE));
            }
            if (query.district != null) {
                rule.add(Restrictions.like("district", query.district, MatchMode.ANYWHERE));
            }
            if (query.userId != null) {
                rule.add(Restrictions.eq("user.id", query.userId));
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }

}
